{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "import tensorflow as tf\n",
    "\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout\n",
    "from keras.optimizers import RMSprop\n",
    "\n",
    "from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 128\n",
    "num_classes = 10\n",
    "epochs = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "x_train = x_train.reshape(60000, 784).astype('float32')\n",
    "x_test = x_test.reshape(10000, 784).astype('float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sc = MinMaxScaler(feature_range=(0,1))\n",
    "\n",
    "x_train = sc.fit_transform(x_train)\n",
    "x_test = sc.fit_transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# one hot encoding\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(Dense(800, activation='relu', input_shape=(784,)))\n",
    "model.add(Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=RMSprop(),\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 14s - loss: 0.2408 - acc: 0.9277 - val_loss: 0.1174 - val_acc: 0.9658\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0945 - acc: 0.9719 - val_loss: 0.0909 - val_acc: 0.9724\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0610 - acc: 0.9813 - val_loss: 0.0669 - val_acc: 0.9797\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0440 - acc: 0.9866 - val_loss: 0.0642 - val_acc: 0.9809\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0319 - acc: 0.9908 - val_loss: 0.0671 - val_acc: 0.9804\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0235 - acc: 0.9931 - val_loss: 0.0645 - val_acc: 0.9811\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0176 - acc: 0.9950 - val_loss: 0.0710 - val_acc: 0.9815\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0132 - acc: 0.9961 - val_loss: 0.0772 - val_acc: 0.9801\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0101 - acc: 0.9971 - val_loss: 0.0721 - val_acc: 0.9826\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0082 - acc: 0.9975 - val_loss: 0.0800 - val_acc: 0.9813\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0061 - acc: 0.9984 - val_loss: 0.0788 - val_acc: 0.9826\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0052 - acc: 0.9984 - val_loss: 0.0836 - val_acc: 0.9830\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0035 - acc: 0.9989 - val_loss: 0.0826 - val_acc: 0.9832\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0031 - acc: 0.9992 - val_loss: 0.0897 - val_acc: 0.9823\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0028 - acc: 0.9992 - val_loss: 0.0827 - val_acc: 0.9833\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0022 - acc: 0.9994 - val_loss: 0.0891 - val_acc: 0.9825\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0020 - acc: 0.9994 - val_loss: 0.0855 - val_acc: 0.9837\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0012 - acc: 0.9997 - val_loss: 0.0921 - val_acc: 0.9830\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0014 - acc: 0.9996 - val_loss: 0.0926 - val_acc: 0.9842\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0010 - acc: 0.9998 - val_loss: 0.0970 - val_acc: 0.9832\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7ffb42d70f98>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train, y_train,\n",
    "                    batch_size=batch_size,\n",
    "                    epochs=epochs,\n",
    "                    verbose=1,\n",
    "                    validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Global TF Kernel (Python 3)",
   "language": "python",
   "name": "global-tf-python-3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
